package com.ambitor.ipro.util;

import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import com.ambitor.grass.util.data.DataMap;
import com.ambitor.grass.util.data.DatasetList;
import com.ambitor.grass.util.data.IData;
import com.ambitor.grass.util.data.IDataset;
import com.ambitor.ipro.entities.Article;
import com.taobao.api.domain.Item;

public class IDataUtils {

	public static void main(String[] args) {
		Article article = new Article();
		article.setARTICLE_ID(1);
		article.setARTICLE_ID(1);
		article.setARTICLE_TITLE("与空姐同居的日子");
		article.setCONTENT("好文章");
		article.setUPDATE_TIME(new Date());
		System.out.println(beanToIData(article));
	}

	public static IData beanToIData(Object obj) {
		if (obj == null)
			return new DataMap();

		JsonConfig cfg = new JsonConfig();
		cfg.registerJsonValueProcessor(java.util.Date.class,
				new JsonValueProcessorImpl());
		JSONObject jsonObj = JSONObject.fromObject(obj,cfg);
		IData data = new DataMap(jsonObj.toString());
		return data;
	}

	@SuppressWarnings("unchecked")
	public static IDataset beansToIDataset(List list) {
		IDataset dataset = new DatasetList();
		if (list == null)
			return dataset;
		for (int i = 0; i < list.size(); i++) {
			IData data = beanToIData(list.get(i));
			if (data != null)
				dataset.add(data);
		}
		return dataset;
	}

	public static IDataset getIDatasetFromJson(String json, String key) {
		if (json == null || key == null)
			return new DatasetList();
		StringBuilder jsonStr = new StringBuilder(json);
		String keys = "\"" + key + "\"" + ":\\[";
		Pattern p = Pattern.compile(keys, Pattern.CASE_INSENSITIVE);
		Matcher m = p.matcher(jsonStr);
		String result = "";
		if (m.find()) {
			int position = m.start();
			jsonStr = jsonStr.delete(0, position);
			result = getSubString(jsonStr.toString());
		}
		return new DatasetList(result);
	}

	private static String getSubString(String jsonStr) {
		jsonStr = jsonStr.substring(jsonStr.indexOf("["));
		int stackIndex = 0;
		for (int i = 0; i < jsonStr.length(); i++) {
			char ch = jsonStr.charAt(i);
			if (ch == '[') {
				++stackIndex;
			}
			if (ch == ']')
				--stackIndex;
			if (stackIndex == 0)
				return jsonStr.substring(0, i + 1);
		}
		return "";

	}
	
	@SuppressWarnings("unchecked")
	public static IDataset itemsListToIDataset(List<Item> items) {
		IDataset dataset = new DatasetList();
		if (items == null || items.size() <= 0)
			return dataset;
		for (Item item : items) {
			IData data = new DataMap();
			data.put("iid", item.getNumIid());
			data.put("num", item.getNum());
			data.put("title", item.getTitle());//jqgrid显示的时候做处理
			data.put("frank_title", item.getTitle());//tilte的本来面目
			data.put("price", item.getPrice());
			data.put("pic_url", item.getPicUrl());
			data.put("delist_time", SkyenccUtil
					.formatDate(item.getDelistTime()));
			data.put("seller_cids", item.getSellerCids());
			data.put("approve_status", item.getApproveStatus());
			dataset.add(data);
		}
		return dataset;
	}
}